<template>
  <div class="todo-example">
    <h2>📝 TodoList示例 - 复杂State管理</h2>
    
    <div class="todo-container">
      <!-- 添加新任务 -->
      <div class="add-todo">
        <input 
          v-model="newTodoText"
          @keyup.enter="addNewTodo"
          placeholder="输入新任务..."
          class="todo-input"
        >
        <button @click="addNewTodo" class="btn btn-primary">添加任务</button>
      </div>
      
      <!-- 任务统计 -->
      <div class="todo-stats">
        <div class="stat-item">
          <span class="stat-number">{{ todoStats.total }}</span>
          <span class="stat-label">总任务</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ todoStats.completed }}</span>
          <span class="stat-label">已完成</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ todoStats.pending }}</span>
          <span class="stat-label">待完成</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ todoStats.completionRate }}%</span>
          <span class="stat-label">完成率</span>
        </div>
      </div>
      
      <!-- 筛选按钮 -->
      <div class="filter-buttons">
        <button 
          @click="currentFilter = 'all'"
          :class="['btn', currentFilter === 'all' ? 'btn-active' : 'btn-secondary']"
        >
          全部 ({{ todos.length }})
        </button>
        <button 
          @click="currentFilter = 'pending'"
          :class="['btn', currentFilter === 'pending' ? 'btn-active' : 'btn-secondary']"
        >
          待完成 ({{ pendingTodos.length }})
        </button>
        <button 
          @click="currentFilter = 'completed'"
          :class="['btn', currentFilter === 'completed' ? 'btn-active' : 'btn-secondary']"
        >
          已完成 ({{ completedTodos.length }})
        </button>
      </div>
      
      <!-- 任务列表 -->
      <div class="todo-list">
        <div 
          v-for="todo in filteredTodos" 
          :key="todo.id"
          :class="['todo-item', { 'completed': todo.done }]"
        >
          <input 
            type="checkbox" 
            :checked="todo.done"
            @change="toggleTodo(todo.id)"
            class="todo-checkbox"
          >
          <span class="todo-text">{{ todo.text }}</span>
          <button 
            @click="deleteTodo(todo.id)" 
            class="btn btn-danger btn-small"
            title="删除任务"
          >
            🗑️
          </button>
        </div>
        
        <div v-if="filteredTodos.length === 0" class="empty-message">
          {{ getEmptyMessage() }}
        </div>
      </div>
      
      <!-- 批量操作 -->
      <div class="bulk-actions" v-if="todos.length > 0">
        <button 
          @click="clearCompleted" 
          :disabled="completedTodos.length === 0"
          class="btn btn-warning"
        >
          清除已完成 ({{ completedTodos.length }})
        </button>
      </div>
    </div>
    
    <!-- 使用说明 -->
    <div class="explanation">
      <h3>🔍 这个示例演示了：</h3>
      <ul class="feature-list">
        <li><strong>复杂State管理:</strong> 管理Todo列表的增删改查</li>
        <li><strong>Getters使用:</strong> 根据状态计算衍生数据（统计信息、筛选结果）</li>
        <li><strong>多个Mutations:</strong> ADD_TODO、TOGGLE_TODO、DELETE_TODO</li>
        <li><strong>批量操作:</strong> 清除已完成任务的批量处理</li>
        <li><strong>数据筛选:</strong> 本地筛选和Vuex数据结合使用</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from 'vuex'

export default {
  name: 'TodoExample',
  data() {
    return {
      newTodoText: '',
      currentFilter: 'all' // all, pending, completed
    }
  },
  computed: {
    // 映射store中的状态和getters
    ...mapState(['todos']),
    ...mapGetters(['completedTodos', 'pendingTodos', 'todoStats']),
    
    // 根据当前筛选条件显示任务
    filteredTodos() {
      switch (this.currentFilter) {
        case 'pending':
          return this.pendingTodos
        case 'completed':
          return this.completedTodos
        default:
          return this.todos
      }
    }
  },
  methods: {
    // 映射store中的actions
    ...mapActions(['addTodo', 'toggleTodo', 'deleteTodo', 'clearCompletedTodos']),
    
    // 添加新任务
    addNewTodo() {
      if (this.newTodoText.trim()) {
        this.addTodo(this.newTodoText)
        this.newTodoText = ''
      }
    },
    
    // 清除已完成任务
    clearCompleted() {
      if (confirm('确定要清除所有已完成的任务吗？')) {
        this.clearCompletedTodos()
        // 如果当前显示已完成任务，切换到全部
        if (this.currentFilter === 'completed') {
          this.currentFilter = 'all'
        }
      }
    },
    
    // 获取空列表提示信息
    getEmptyMessage() {
      switch (this.currentFilter) {
        case 'pending':
          return '🎉 所有任务都已完成！'
        case 'completed':
          return '还没有完成任何任务'
        default:
          return '还没有任务，赶快添加一个吧！'
      }
    }
  }
}
</script>

<style scoped>
.todo-example {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.todo-container {
  background: #fff;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.add-todo {
  display: flex;
  gap: 10px;
  margin-bottom: 30px;
}

.todo-input {
  flex: 1;
  padding: 12px 16px;
  border: 2px solid #dee2e6;
  border-radius: 6px;
  font-size: 1rem;
}

.todo-input:focus {
  outline: none;
  border-color: #007bff;
}

.todo-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 15px;
  margin-bottom: 25px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  display: block;
  font-size: 2rem;
  font-weight: bold;
  color: #2c3e50;
}

.stat-label {
  font-size: 0.9rem;
  color: #6c757d;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.filter-buttons {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background: #007bff;
  color: white;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-active {
  background: #28a745 !important;
  color: white;
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn-warning {
  background: #ffc107;
  color: #212529;
}

.btn-small {
  padding: 4px 8px;
  font-size: 0.8rem;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.todo-list {
  min-height: 200px;
}

.todo-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  margin-bottom: 10px;
  transition: all 0.3s ease;
}

.todo-item:hover {
  background: #f8f9fa;
}

.todo-item.completed {
  background: #d4edda;
  border-color: #c3e6cb;
}

.todo-checkbox {
  margin-right: 15px;
  transform: scale(1.2);
}

.todo-text {
  flex: 1;
  font-size: 1rem;
}

.todo-item.completed .todo-text {
  text-decoration: line-through;
  color: #6c757d;
}

.empty-message {
  text-align: center;
  padding: 40px;
  color: #6c757d;
  font-size: 1.1rem;
}

.bulk-actions {
  margin-top: 20px;
  text-align: center;
}

.explanation {
  background: #f8f9fa;
  padding: 25px;
  border-radius: 12px;
  border-left: 5px solid #28a745;
}

.explanation h3 {
  color: #2c3e50;
  margin-bottom: 15px;
}

.feature-list {
  list-style: none;
  padding: 0;
}

.feature-list li {
  padding: 8px 0;
  border-bottom: 1px solid #dee2e6;
}

.feature-list li:last-child {
  border-bottom: none;
}

.feature-list strong {
  color: #495057;
}
</style>